﻿using Learn.Infraestrutura.Attr;
using Learn.Infraestrutura.Dto.Request.Org;
using Learn.Infraestrutura.Dto.Response;
using Learn.Infraestrutura.Dto.Response.Org;
using Learn.Infraestrutura.EFCore;
using Learn.Infraestrutura.IOC;
using Learn.Service.EFCore;
using Learn.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Learn.Service.Instance
{
    [Inject_]
    public class Org_Service : Base_Service, I_Org_Service
    {
        private readonly Org_IOC _org_IOC;

        public Org_Service(Org_IOC org_IOC)
        {
            _org_IOC = org_IOC;
        }

        /// <summary>
        /// 获取部门
        /// </summary>
        /// <returns></returns>
        public Api_Result Get_Dept(Org_Request_Dto dto)
        {
            var depts = _org_IOC._sys_Dept_EFCore.QueryAll(d =>
              (d.DisBan == 0)
              ).ToList();
            var root = depts.Where(d => d.Dparent == null).ToList();
            List<Department_Response_Dto> data = new List<Department_Response_Dto>();
            //获取各个部门的人数
            var users = _org_IOC._sys_User_EFCore.QueryAll(d => d.Udept != null).ToList();

            root.ForEach(d =>
            {
                var dep = _org_IOC._sys_Dept_EFCore.QueryAll(e => e.Dparent == d.Did).ToList();
                data.Add(new Department_Response_Dto
                {
                    id = d.Did,
                    count = users.Count(e => e.Udept == d.Did),
                    explain = d.Dexplain,
                    isBan = d.DisBan,
                    name = d.Dname,
                    time = d.DcreateTime.ToString(),
                    icon = d.Dicon,
                    children = Get_Children_Dept(d.Did, depts, users)
                });
            });
            return Result(1, "ok", data: data);
        }
        /// <summary>
        /// 获取子集部门
        /// </summary>
        /// <param name="did"></param>
        /// <param name="depts"></param>
        /// <param name="users"></param>
        /// <returns></returns>
        private IEnumerable<Department_Response_Dto> Get_Children_Dept(string did, List<SysDepartment> depts, List<SysUsers> users)
        {
            var root = depts.Where(d => d.Dparent == did).ToList();
            List<Department_Response_Dto> data = new List<Department_Response_Dto>();



            root.ForEach(d =>
            {
                data.Add(new Department_Response_Dto
                {
                    id = d.Did,
                    count = users.Count(e => e.Udept == d.Did),//users.Where(e=>e.UDeptID==d.DID).Count(),
                    explain = d.Dexplain,
                    isBan = d.DisBan,
                    name = d.Dname,
                    time = d.DcreateTime.ToString(),
                    icon = d.Dicon,
                    children = Get_Children_Dept(d.Did, depts, users)
                });
            });
            return data;
        }
        /// <summary>
        /// 获取所有职务
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Api_Result Get_Duties(Org_Request_Dto dto)
        {
            dto.key = dto.key ?? "";
            

            var data = _org_IOC._sys_Duties_EFCore.QueryAll(out int total, dto.pages, dto.limit, true, d => d.DcreateTime, d => d.Dname.Contains(dto.key)).
                Select(d => new Duties_Response_Dto
                {
                    id = d.Did,
                    isBan = d.DisBan,
                    lv = d.Dlv,
                    name = d.Dname,
                    createTime = d.DcreateTime
                }).ToList();
            data.ForEach(d =>
            {
                d.count = _org_IOC._sys_User_EFCore.QueryAll(e => e.Uduty == d.id).Count();
                d.time = d.createTime.Value.ToString("yyyy/MM/dd HH:mm");
            });

            return Result(1, "ok", data: new { total = total, data = data });
        }

        /// <summary>
        /// 新增职务
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Api_Result Add_Duties(Duties_Request_Dto dto)
        {

            //if (dto.dutyName == null)
            //{
            //    return Result(0, "请输入名字");
            //}
            //Sys_Duties_EFCore duties = new Sys_Duties_EFCore()
            //{
            //    DcreateTime = DateTime.Now,
            //    DID = Config.GUID_2(),
            //    DExplain = dto.dutyExplain,
            //    DIsBan = 0,
            //    DLv = dto.dutyLv,
            //    DName = dto.dutyName,
            //    DPersonnel = 10,
            //};
            //_sys_Duties_EF.Add(duties);
            //var result = _sys_Duties_EF.SaveChanges();

            //return Result(result ? 0 : -1, result ? "ok" : "失败");

            return null;
        }
        /// <summary>
        /// 获取职务详情
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Api_Result Get_Duties_Details(Duties_Request_Dto dto)
        {
            var iq = _org_IOC._sys_Duties_EFCore.QueryAll(d => d.Did == dto.id);
            if (!iq.Any())
            {
                return Result(-1, "参数错误");
            }
            var duties = iq.Single();
            Duties_Response_Dto data = new Duties_Response_Dto()
            {
                id = duties.Did,
                count = _org_IOC._sys_User_EFCore.QueryAll(d => d.Uduty == dto.id).Count(),
                isBan = duties.DisBan,
                lv = duties.Dlv,
                name = duties.Dname,
                time = duties.DcreateTime.Value.ToString(),
                explain = duties.Dexplain,
            };
            return Result(1, "ok", data);

        }
        /// <summary>
        /// 修改职务
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Api_Result Update_Duties(Duties_Request_Dto dto)
        {
            var iq = _org_IOC._sys_Duties_EFCore.QueryAll(d => d.Did == dto.id);
            if (!iq.Any())
            {
                return Result(-1, "参数错误");
            }
            var duty = iq.Single();
            duty.Dlv = dto.lv;
            duty.Dname = dto.name;
            duty.Dexplain = dto.explain;
            duty.DisBan = dto.isBan;
            var result = _org_IOC._sys_Duties_EFCore.SaveChanges();
            return Result(result, result>0 ? "修改成功" : "修改失败");
        }
        /// <summary>
        /// 删除职务
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public Api_Result Delete_Duties(Duties_Request_Dto dto)
        {
            var iq = _org_IOC._sys_Duties_EFCore.QueryAll(d => d.Did == dto.id).Single();
            _org_IOC._sys_Duties_EFCore.Delete(iq);
            var result = _org_IOC._sys_Duties_EFCore.SaveChanges();
            return Result(result, result>0 ? "删除成功" : "删除失败");
        }
    }
}
